package com.uplooking.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.uplooking.service.ChartService;

@Service("chartService")
public class ChartServiceImpl implements ChartService {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public Map<String, Object> getBusCount() {
		String sql = "SELECT rtbsno name,COUNT(rtbsno) value FROM tblrent GROUP BY rtbsno;";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		System.out.println(list);
		Map<String, Object> map = new HashMap<>();
		String[] name = new String[list.size()];
		String[] value = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = list.get(i).get("name").toString();
			value[i] = list.get(i).get("value").toString();
		}
		map.put("name", name);
		map.put("value", value);
		return map;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public Map<String, Object> getCustCount() {
		String sql = "SELECT rtctid name,COUNT(rtctid) value FROM tblrent GROUP BY rtctid;";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		System.out.println(list);
		Map<String, Object> map = new HashMap<>();
		String[] name = new String[list.size()];
		String[] value = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = list.get(i).get("name").toString();
			value[i] = list.get(i).get("value").toString();
		}
		map.put("name", name);
		map.put("value", value);
		return map;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public Map<String, Object> getCustMoney() {
		String sql = "SELECT rnuname name,SUM(rncost) value FROM tblreturn GROUP BY rnuname;";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		System.out.println(list);
		Map<String, Object> map = new HashMap<>();
		String[] name = new String[list.size()];
		String[] value = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = list.get(i).get("name").toString();
			value[i] = list.get(i).get("value").toString();
		}
		map.put("name", name);
		map.put("value", value);
		return map;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public Map<String, Object> getCustMoneyByMonth() {
		String sql = "SELECT rnuname name,SUM(rncost) value FROM tblreturn GROUP BY rnuname;";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		System.out.println(list);
		Map<String, Object> map = new HashMap<>();
		String[] name = new String[list.size()];
		String[] value = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = list.get(i).get("name").toString();
			value[i] = list.get(i).get("value").toString();
		}
		map.put("name", name);
		map.put("value", value);
		return map;
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public Map<String, Object> getCustMoneyByYear() {
		String sql = "SELECT rnuname name,SUM(rncost) value FROM tblreturn GROUP BY rnuname;";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		System.out.println(list);
		Map<String, Object> map = new HashMap<>();
		String[] name = new String[list.size()];
		String[] value = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			name[i] = list.get(i).get("name").toString();
			value[i] = list.get(i).get("value").toString();
		}
		map.put("name", name);
		map.put("value", value);
		return map;
	}

}
